home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ident / tools / tcplist-1.1.shar / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-14  |  4.6 KB  |  251 lines

  1. /*
  2.  * John DiMarco   University of Toronto, CSRI
  3.  */
  4. #include <stdio.h>
  5. #include <strings.h>
  6. #include <varargs.h>
  7. #include "utils.h"
  8.  
  9. #ifdef lint
  10. #undef va_arg(x,y)
  11. #define va_arg(x,y) (y)NULL
  12. #endif
  13.  
  14. extern char *progname;
  15. extern int d;
  16.  
  17. extern char *malloc(), *realloc();
  18.  
  19. /*
  20.  * Error(): behaves like fprintf(stderr, ...) followed by exit(2), except
  21.  *          that the 'programname: ' preceeds the print, and a newline
  22.  *        follows it.
  23.  */
  24. /*VARARGS*/
  25. void Error(va_alist)
  26. va_dcl
  27. {
  28.     va_list args;
  29.     char *format;
  30.  
  31.     va_start(args);
  32.     format = va_arg(args, char *);
  33.     fprintf(stderr, "%s: ", progname);
  34.     vfprintf(stderr, format, args);
  35.     fprintf(stderr, "\n");
  36.     va_end(args);
  37.     (void)exit(2);
  38. }
  39.  
  40. /*
  41.  * dfprintf(): behaves like fprintf, except the first argument must be a
  42.  *            debugging level. The message will only be printed if "d"
  43.  *            is equal to or greater than the debugging level.
  44.  */
  45. /*VARARGS*/
  46. void dfprintf(va_alist)
  47. va_dcl
  48. {
  49.     va_list args;
  50.     int debugLevel;
  51.     FILE *stream;
  52.     char *format;
  53.     
  54.     va_start(args);
  55.     debugLevel = va_arg(args, int);
  56.     stream = va_arg(args, FILE *);
  57.     format = va_arg(args, char *);
  58.     if(d >= debugLevel){
  59.         vfprintf(stream, format, args);
  60.     }
  61.     va_end(args);
  62. }
  63.     
  64. /*
  65.  * Warning(): behaves like Error, except returns rather than exits.
  66.  */
  67. /*VARARGS*/
  68. void Warning(va_alist)
  69. va_dcl
  70. {
  71.     va_list args;
  72.     char *format;
  73.  
  74.     va_start(args);
  75.     format = va_arg(args, char *);
  76.     fprintf(stderr, "%s: ", progname);
  77.     vfprintf(stderr, format, args);
  78.     fprintf(stderr, "\n");
  79.     va_end(args);
  80. }
  81.  
  82. FILE *efopen(file, mode)
  83. char *file, *mode;
  84. {
  85.     FILE *fp;
  86.     if (NULL!=(fp=fopen(file,mode)))
  87.         return(fp);
  88.     Error("can't open file \"%s\" mode \"%s\"", file, mode);
  89.     /*NOTREACHED*/
  90. }
  91.  
  92. void efclose(f)
  93. FILE *f;
  94. {
  95.     if(EOF==fclose(f))
  96.         Error("can't close file");
  97.     /*NOTREACHED*/
  98. }
  99.  
  100. /*
  101.  * mylib_malloc(): Checks if it gets a NULL pointer, calls Error if so.
  102.  */
  103. char *mylib_malloc(size, file, line)
  104. unsigned size;
  105. char *file;
  106. int line;
  107. {
  108.     char *result;
  109.  
  110.     result = malloc(size);
  111.     if(NULL==result){
  112.         Error("Out of memory at line %d in \"%s\".", line, file);
  113.     }
  114.     return(result);
  115. }
  116.  
  117. /*
  118.  * mylib_realloc(): Checks if it gets a NULL pointer, calls Error if so.
  119.  */
  120. char *mylib_realloc(ptr, size, file, line)
  121. char *ptr, *file;
  122. unsigned size;
  123. int line;
  124. {
  125.     char *result;
  126.  
  127.     result = realloc(ptr, size);
  128.     if(NULL==result){
  129.         Error("Out of memory at line %d in \"%s\".", line, file);
  130.     }
  131.     return(result);
  132. }
  133.  
  134. /*
  135.  * mylib_scopy(): Takes a string and creates a new physical copy of it.
  136.  */
  137. char *mylib_scopy(string, file, line)
  138. char *string, *file;
  139. int line;
  140. {
  141.     char *result;
  142.  
  143.     result = malloc((unsigned)strlen(string)+1);
  144.  
  145.     if(NULL==result){
  146.         Error("Out of memory at line %d in \"%s\".", line, file);
  147.     }
  148.     (void)strcpy(result, string);
  149.     return(result);
  150. }
  151.  
  152. /*
  153.  * mylib_srcopy(): Reallocs first string to make room for second, copies it.
  154.  */
  155. char *mylib_srcopy(s1, s2, file, line)
  156. char *s1, *s2, *file;
  157. int line;
  158. {
  159.     s1=mylib_realloc(s1, (unsigned)strlen(s2)+1, file, line);
  160.     (void)strcpy(s1, s2);
  161.     return(s1);
  162. }
  163.  
  164. /*
  165.  * cat(): Take a list of strings, followed by NULL, return their concatenation
  166.  *        in malloc'ed space.
  167.  */
  168. /*VARARGS*/
  169. char *cat(va_alist)
  170. va_dcl
  171. {
  172.     va_list args;
  173.     unsigned length=1;
  174.     char *str, *newstr;
  175.  
  176.     /* get length */
  177.     va_start(args);
  178.     while(1){
  179.         str = va_arg(args, char *);
  180.         if(NULL!=str){
  181.              length+=strlen(str);
  182.         } else {
  183.             break;
  184.         }
  185.     }
  186.     va_end(args);
  187.  
  188.     newstr=malloc(length);
  189.     if(NULL==newstr) Error("Out of memory in cat()");
  190.     
  191.     newstr[0]=(char)0;
  192.  
  193.     /* create string */
  194.     va_start(args);
  195.     while(1){
  196.         str = va_arg(args, char *);
  197.         if(NULL!=str) {
  198.             (void)strcat(newstr, str);
  199.         } else {
  200.             break;
  201.         }
  202.     }
  203.  va_end(args);
  204. #ifdef lint
  205.     args=args; /* make lint shut up about "args set but not used" */
  206. #endif
  207.     return(newstr);
  208. }
  209.  
  210. /*
  211.  * getstr(): read a string of arbitrary length from the given file 
  212.  *           descriptor into malloc'ed memory, up to (but not including)
  213.  *           the next newline. Return a pointer to the string; NULL if EOF.
  214.  */
  215. char *getstr(fd)
  216. FILE *fd;
  217. {
  218.     unsigned int buffsize = 128; /* buffer size */
  219.     char *result, *tmp;
  220.     unsigned int length = 0; /* length of string read */
  221.     int last;
  222.     
  223.     result = mem(buffsize+1);
  224.  
  225.     loop{
  226.         tmp = result+length;
  227.         if(NULL==fgets(tmp, buffsize+1, fd)){
  228.             /* no more characters to read; EOF reached */
  229.             if(tmp==result){
  230.                 /* we never read anything! */
  231.                 free(result);
  232.                 return(NULL);
  233.             }
  234.             break;
  235.         } else {
  236.             last = strlen(tmp);
  237.             if('\n'==*(tmp+last-1)){
  238.                 /* found a newline */
  239.                 *(tmp+last-1)='\0';
  240.                 break;
  241.             }
  242.             /* still more to read */
  243.             length += buffsize;
  244.             realloc(result, length+buffsize);
  245.         }
  246.     }
  247.     /* trim off excess buffer */
  248.     rmem(result, strlen(result)+1);
  249.     return(result);
  250. }
  251.